package com.gzcsqic.excelapp.service;

import cn.hutool.core.io.FileUtil;
import com.gzcsqic.excelapp.common.Constants;
import com.gzcsqic.excelapp.utils.ExcelUtil;
import com.gzcsqic.excelapp.utils.OsUtil;
import com.gzcsqic.excelapp.utils.PrintUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.*;

@Service
public class SystemService {

    static Logger log = LoggerFactory.getLogger(SystemService.class);

    // 获取系统打印机参数
    public Map<String,Object> getPrinters(){
        Map<String,Object> data = new HashMap<>();
        String defaultPrinterName = PrintUtil.getDefaultPrinterName();
        List<String> printerNames = PrintUtil.getPrinterNames();
        data.put("default",defaultPrinterName);
        data.put("list",printerNames);
        return data;
    }

    // 获取净度参数
    public List<Map<String,String>> getSiParams(){
        List<Map<String,String>> list = new LinkedList<>();
        try {
            File file = new File(OsUtil.getPath() + "param/si_param.xlsx");
            list = ExcelUtil.readData(file.getPath(), "Sheet1");
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(list != null && list.size() > 0){
            list.remove(0);
        }
        return list;
    }

    // 获取牙率参数
    public List<Map<String,String>> getGrParams(){
        List<Map<String,String>> list = new LinkedList<>();
        try {
            File file = new File(OsUtil.getPath() + "param/gr_param.xlsx");
            list = ExcelUtil.readData(file.getPath(), "Sheet1");
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(list != null && list.size() > 0){
            list.remove(0);
        }
        return list;
    }

    // 获取样品参考参数
    public List<Map<String,String>> getTestParams(){
        List<Map<String,String>> list = new LinkedList<>();
        try {
            File file = new File(OsUtil.getPath() + "param/test_param.xlsx");
            list = ExcelUtil.readData(file.getPath(), "参数");
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(list != null && list.size() > 0){
            list.remove(0);
        }
        return list;
    }

    // 获取人员
    public List<Map<String,String>> getEmployees(){
        List<Map<String,String>> list = new LinkedList<>();
        try {
            File file = new File(OsUtil.getPath() + Constants.EMP_PARAM_FILE);
            list = ExcelUtil.readData(file.getPath(), "Sheet1");
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(list != null && list.size() > 0){
            list.remove(0);
        }
        return list;
    }

    public void shutdown() {
        String scriptPath = OsUtil.getPath() + "bin";
        String cmd = "cmd.exe /c start /b shutdown.bat";
        Process ps = null;
        try {
            log.info(scriptPath);
            ps = Runtime.getRuntime().exec(cmd,null,new File(scriptPath));
            ps.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        } catch (InterruptedException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }
}
